package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveOrderFieldSvc;
import com.kmxd.ams.client.archive.dto.ArchiveOrderFieldAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveOrderFieldEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveOrderFieldQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveFieldListVO;
import com.kmxd.ams.client.archive.vo.ArchiveOrderFieldListVO;
import com.kmxd.ams.client.archive.vo.ArchiveOrderFieldVO;
import com.kmxd.ams.core.common.constant.CacheConst;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveOrderField;
import com.kmxd.ams.infra.archive.mapper.ArchiveOrderFieldMapper;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 整理库档案数据排序字段 服务实现类
 *
 * @author nickbi
 * @since 2022-07-28
 */
@Service
@AllArgsConstructor
@CacheConfig(cacheNames = CacheConst.CACHE_PREFIX_ARCHIVE_RULE_CATALOG)
public class ArchiveOrderFieldSvcImpl
    extends ServiceImpl<ArchiveOrderFieldMapper, ArchiveOrderField>
    implements IArchiveOrderFieldSvc {

  private ArchiveOrderFieldMapper archiveOrderFieldMapper;

  @Cacheable(keyGenerator = "myKeyGenerator")
  @Override
  public Page<ArchiveOrderFieldListVO> selectPage(Page reqPage, ArchiveOrderFieldQueryDTO req) {
    LambdaQueryWrapper<ArchiveOrderField> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveOrderField>(), req).lambda();
    queryWrapper.orderByAsc(ArchiveOrderField::getSort);
    Page<ArchiveOrderField> page = archiveOrderFieldMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveOrderFieldListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveOrderFieldListVO.class));
    return voPage;
  }

  @Cacheable(keyGenerator = "myKeyGenerator")
  @Override
  public List<ArchiveOrderFieldListVO> selectList(ArchiveOrderFieldQueryDTO req) {
    LambdaQueryWrapper<ArchiveOrderField> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveOrderField>(), req).lambda();
    queryWrapper.orderByAsc(ArchiveOrderField::getSort);
    List<ArchiveOrderField> list = archiveOrderFieldMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveOrderFieldListVO.class);
  }

  @Cacheable(keyGenerator = "myKeyGenerator")
  @Override
  public List<ArchiveOrderFieldListVO> selectListByIds(Set<Long> ids) {
    LambdaQueryWrapper<ArchiveOrderField> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.in(Objects.nonNull(ids), ArchiveOrderField::getId, ids);
    List<ArchiveOrderField> list = archiveOrderFieldMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveOrderFieldListVO.class);
  }

  @CacheEvict(allEntries = true)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveOrderFieldAddDTO req) {
    ArchiveOrderField entity = BeanUtil.copyProperties(req, ArchiveOrderField.class);
    archiveOrderFieldMapper.insert(entity);
  }

  @CacheEvict(allEntries = true)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveOrderFieldEditDTO req) {
    ArchiveOrderField entity = BeanUtil.copyProperties(req, ArchiveOrderField.class);
    archiveOrderFieldMapper.updateById(entity);
  }

  @CacheEvict(allEntries = true)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveFieldListVO getAutoIndex(ArchiveOrderFieldEditDTO req) {
    LambdaQueryWrapper<ArchiveOrderField> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.eq(ArchiveOrderField::getTableId, req.getTableId());
    queryWrapper.eq(ArchiveOrderField::getArchTypeId, req.getArchTypeId());
    queryWrapper.orderByDesc(ArchiveOrderField::getSort);
    queryWrapper.last("limit 1");
    ArchiveOrderField one = this.getOne(queryWrapper);
    if (ObjectUtil.isEmpty(one)) {
      one = new ArchiveOrderField();
    }
    one.setSort(
        (ObjectUtil.isEmpty(one) || ObjectUtil.isEmpty(one.getSort()) ? 1 : one.getSort()) + 1);
    return BeanUtil.copyProperties(one, ArchiveFieldListVO.class);
  }

  @CacheEvict(allEntries = true)
  @Override
  public ArchiveOrderFieldVO view(String id) {
    ArchiveOrderField entity = archiveOrderFieldMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveOrderFieldVO.class);
  }
}
